package com.zfsoft.zfoa.onduty.wage.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.struts.util.LabelValueBean;

import com.zfsoft.zfoa.PublicClass;
import com.zfsoft.zfoa.onduty.timetable.dao.TimetableDAO;
import com.zfsoft.zfoa.onduty.timetable.form.TimetableForm;
import com.zfsoft.zfoa.onduty.timetable.entitys.Timetable;
import com.zfsoft.zfoa.onduty.timetable.parameter.TimetableParameter;
import com.zfsoft.zfoa.onduty.wage.dao.WagechildDAO;
import com.zfsoft.zfoa.onduty.wage.dao.WagefatherDAO;
import com.zfsoft.zfoa.onduty.wage.entitys.Wagechild;
import com.zfsoft.zfoa.onduty.wage.entitys.Wagefather;
import com.zfsoft.zfoa.onduty.wage.form.WagefatherForm;
import com.zfsoft.dao.DAO;
import com.zfsoft.util.Tool;
import com.zfsoft.zfoa.action.BaseAction;
import com.zfsoft.zfoa.dao.sysman.MxdxDao;

public class WagefatherAction extends BaseAction {

	private WagefatherDAO wagefatherDao;
	private WagechildDAO wagechildDao;
	
	public void edit() throws Exception {
		List<LabelValueBean> months = new ArrayList<LabelValueBean>();
		months.add(new LabelValueBean("1","1"));
		months.add(new LabelValueBean("2","2"));
		months.add(new LabelValueBean("3","3"));
		months.add(new LabelValueBean("4","4"));
		months.add(new LabelValueBean("5","5"));
		months.add(new LabelValueBean("6","6"));
		months.add(new LabelValueBean("7","7"));
		months.add(new LabelValueBean("8","8"));
		months.add(new LabelValueBean("9","9"));
		months.add(new LabelValueBean("10","10"));
		months.add(new LabelValueBean("11","11"));
		months.add(new LabelValueBean("12","12"));
		request.setAttribute("months", months);
		WagefatherForm form=(WagefatherForm) this.form;
		if(!Tool.isNull(form.getParameter().getId())){
			Wagefather wagefather =wagefatherDao.getInstance(form.getParameter().getId());
			form.setWagefather(wagefather);
			//form.setWagechilds(wagechildDao.getListByFid(form.getParameter().getId()));
			request.setAttribute("wagechilds", wagechildDao.getListByFid(form.getParameter().getId()));
		}else{
			
			Wagefather wagefather =new Wagefather();
			form.setWagefather(wagefather);
			String dxid =request.getParameter("dxid");
			String mxdxid=PublicClass.setMxdx(dxid);
			List<HashMap<String,String>> list =new MxdxDao().getUserListByDXIDED(mxdxid);
			List<Wagechild> wagechilds =new ArrayList<Wagechild>();
			for(int i=0;i<list.size();i++){
				HashMap<String,String> map =list.get(i);
				String yhm = map.get("YHM");
				String xm = new DAO().getOneRs("select xm from yhb where yhm='"+yhm+"'");
				Wagechild wagechild =new Wagechild();
				wagechild.setUserid(yhm);
				wagechild.setUsername(xm);
				wagechilds.add(wagechild);
			}
			request.setAttribute("wagechilds", wagechilds);
		}
		this.forward="edit";
	}
	public void windowpickperson(){
		this.forward="windowpickperson";
	}
	public WagefatherDAO getWagefatherDao() {
		return wagefatherDao;
	}

	public void setWagefatherDao(WagefatherDAO wagefatherDao) {
		this.wagefatherDao = wagefatherDao;
	}

	public WagechildDAO getWagechildDao() {
		return wagechildDao;
	}

	public void setWagechildDao(WagechildDAO wagechildDao) {
		this.wagechildDao = wagechildDao;
	}

	public void view() throws Exception {}

	public void save() throws Exception {
		WagefatherForm form=(WagefatherForm) this.form;
		List<Wagechild> wagechilds =new ArrayList<Wagechild>();
		String[] childs=request.getParameter("childs").split(",");
		for(int i=0;i<childs.length;i+=10){
			Wagechild wagechild =new Wagechild();
			wagechild.setId(childs[i]);
			wagechild.setUserid(childs[i+1]);
			wagechild.setUsername(childs[i+2]);
			wagechild.setAllday(childs[i+3]);
			wagechild.setAllmoney(childs[i+4]);
			wagechild.setDaytime(childs[i+5]);
			wagechild.setDaymoney(childs[i+6]);
			wagechild.setNighttime(childs[i+7]);
			wagechild.setNightmoney(childs[i+8]);
			wagechild.setSum(childs[i+9]);
			wagechilds.add(wagechild);
		}
		
		
		Wagefather wagefather =form.getWagefather();
		if(wagefather.getAssessor().indexOf("-")!=-1){
			wagefather.setAssessor(wagefather.getAssessor().split("-")[1]);
		}
		if(Tool.isNull(wagefather.getId())){
			wagefather.setId(Tool.getId());
			wagefatherDao.insert(wagefather);
			for(int i=0;i<wagechilds.size();i++){
				Wagechild child = wagechilds.get(i);
				if(Tool.isNull(child.getId())){
					child.setFid(wagefather.getId());
					child.setId(Tool.getId());
					wagechildDao.insert(child);
				}else{
					wagechildDao.update(child.getId(), child);
				}
			}
		}else{
			wagefatherDao.update(wagefather.getId(), wagefather);
			for(int i=0;i<wagechilds.size();i++){
				Wagechild child = wagechilds.get(i);
				if(Tool.isNull(child.getId())){
					child.setFid(wagefather.getId());
					wagechildDao.insert(child);
				}else{
					child.setFid(wagefather.getId());
					wagechildDao.update(child.getId(), child);
				}
			}
		}
		this.forward="list";
	}

	public void list() throws Exception {
		this.forward="list";
	}

	public void delete() throws Exception {}

}
